package com.lcxw.hxzbapi.controller.giftandcubecontroller;

import com.lcxw.hxzbapi.controller.membercontroller.MemberRealization;
import com.lcxw.hxzbapi.controller.roomcontroller.RoomRealization;
import com.lcxw.hxzbapi.entity.pojo.Gift;
import com.lcxw.hxzbapi.entity.pojo.Ticket;
import com.lcxw.hxzbapi.entity.pojo.YwpdGiftBag;
import com.lcxw.hxzbapi.entity.pojo.YwpdRoom;
import com.lcxw.hxzbapi.service.GiftBagService;
import com.lcxw.hxzbapi.service.TicketService;
import com.lcxw.hxzbapi.service.YwpdConsumeService;
import com.lcxw.hxzbapi.service.YwpdMemberService;
import com.lcxw.hxzbapi.service.YwpdRoomService;
import java.lang.reflect.Member;
import javax.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.stereotype.Component;
import com.lcxw.hxzbapi.entity.pojo.GiftSend;
import com.lcxw.hxzbapi.service.GiftSendService;
import com.lcxw.hxzbapi.service.GiftService;
import com.lcxw.hxzbcommon.common.StringUtil;

@Component
@Slf4j
public class GiftRealization {

    @Resource
    GiftSendService giftSendService;

    @Resource
    YwpdMemberService ywpdMemberService;

    @Resource
    GiftService giftService;

    @Resource
    GiftBagService giftBagService;

    @Resource
    YwpdRoomService roomService;

    @Resource
    MemberRealization memberRealization;

    @Resource
    RoomRealization roomRealization;

    @Resource
    TicketService ticketService;

    public int sendGift(long roomId, long userId, long fromUserId, int giftId, int giftNum,
        String source) throws Exception {

        GiftSend giftSend = new GiftSend();

        giftSend.setRoomId(roomId);
        giftSend.setAddTime(StringUtil.getDateTime());
        giftSend.setUserId(userId);
        giftSend.setFromUserId(fromUserId);
        giftSend.setGiftId(giftId);
        giftSend.setGiftNum(giftNum);
        giftSend.setSource(source);

        Gift gift = giftService.selectByPrimaryKey(giftId);
        int needCoin = gift.getNeedCoin();
        giftSend.setNeedCoin(needCoin);

        log.info("用户 {} 给用户 {} 赠送 {} 个礼物，礼物Id为 {}", fromUserId, userId, giftNum, giftId);

        int spendCoin = spendCoin(giftId, giftNum);
        int earnBean = ywpdMemberService.selectYwpdMemberByUserId(fromUserId).getEarnBean();

        //如果是从背包中送过来的礼物
        if ("bag".equals(source)) {
            YwpdGiftBag giftBag = giftBagService.selectByUserIdAndGiftId(userId, giftId);
            //如果背包中的礼物数量不足，则返回-1
            if (null == giftBag || giftBag.getGiftNum() < giftNum) {
                return -1;
            }
            //如果背包中的礼物数量刚好够，则删除该记录
            if (giftNum - giftBag.getGiftNum() == 0) {
                giftBagService.deleteByPrimaryKey(giftBag.getId());
            }
            giftBagService.updateGiftNum(userId, giftId, -giftNum);
            giftSendService.insertGiftEarn(giftSend);//将该记录加入到礼物赠送里面
            roomRealization.addFire(roomId, spendCoin);//房间添加火力值
            memberRealization.addConsume(fromUserId, spendCoin);//将礼物赠送的金额加入到消费金额中
            //贵重礼物赠送的通知
            if (needCoin >= 5000) {
                roomRealization.valuableGiftSend(userId, fromUserId, giftId);
            }
            return 0;
        }

        //如果送礼的人拥有的虚拟货币足够送礼，则完成送礼，并且扣除虚拟货币数量，返回值为 0
        if (earnBean >= spendCoin) {

//            //判断该房间是否是拍拍，如果是的话，需要将赠送礼物的价值加入到爆灯统计
//            YwpdRoom ywpdRoom = roomService.selectRoomByRoomId(roomId);
//            if ("paipai".equals(ywpdRoom.getRoomType())){
//                log.info("用户在拍拍房间中花费了"+spendCoin+"金币，需要加入到爆灯统计中");
//                roomRealization.setBaodeng(roomId,spendCoin);
//            }

            ywpdMemberService.updateCoinByUserId(-spendCoin, fromUserId);
            giftSendService.insertGiftEarn(giftSend);

            //这里将花费的虚拟币计入房间的火力值，并推送给前端
            roomRealization.addFire(roomId, spendCoin);

            //将礼物赠送的金额加入到消费金额中
            memberRealization.addConsume(fromUserId, spendCoin);
            //贵重礼物赠送的通知
            if (needCoin >= 5000) {
                roomRealization.valuableGiftSend(userId, fromUserId, giftId);
            }
            return 0;
        }

        //如果虚拟货币不够，则返回需要多少虚拟货币
        return spendCoin - earnBean;
    }

    //获取这次送礼所需要的的虚拟货币值
    public int spendCoin(int giftId, int giftNum) throws Exception {
        int needCoin = giftService.selectByPrimaryKey(giftId).getNeedCoin();
        return needCoin * giftNum;
    }

    //魔方礼物赠送后的抽奖券增加
    public void addTicket(long userId, int giftNum, String cubeType) {
        if (ticketService.selectByUserId(userId) != null) {
            if ("luckyCube".equals(cubeType)) {
                ticketService.updateLuckyByUserId(userId, giftNum);
            } else if ("diamondCube".equals(cubeType)) {
                ticketService.updateDiamondByUserId(userId, giftNum);
            }
            return;
        }

        Ticket ticket = new Ticket();
        ticket.setUserId(userId);
        if ("luckyCube".equals(cubeType)) {
            ticket.setLucky(giftNum);
        } else if ("diamondCube".equals(cubeType)) {
            ticket.setDiamond(giftNum);
        }
        ticketService.insert(ticket);
    }

    //获取魔方抽奖次数
    public int getTicketTimes(long userId, String cubeType) {

        if (ticketService.selectByUserId(userId) == null) {
            return 0;
        }

        Ticket ticket = ticketService.selectByUserId(userId);
        if ("luckyCube".equals(cubeType)) {
            return ticket.getLucky();
        } else if ("diamondCube".equals(cubeType)) {
            return ticket.getDiamond();
        }

        return 0;
    }

    //魔方抽奖后，减掉抽奖券
    public void minusTicket(long userId, String cubeType, int times) {
        times = -times;

        if (ticketService.selectByUserId(userId) != null) {
            if ("luckyCube".equals(cubeType)) {
                ticketService.updateLuckyByUserId(userId, times);
            } else if ("diamondCube".equals(cubeType)) {
                ticketService.updateDiamondByUserId(userId, times);
            }
        }
    }
}
